package com.app.milkteaapp.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Button;

import com.app.milkteaapp.bean.Cart;
import com.app.milkteaapp.R;
import com.app.milkteaapp.bean.FindCartGsonBean;
import com.app.milkteaapp.fragment.ShopCartFragment;
import com.app.milkteaapp.utils.MyTools;
import com.squareup.picasso.Picasso;

public class CartAdapter extends BaseAdapter {
    private static final String TAG = "CartAdapter";
    private List<Cart> objects = new ArrayList<>();
    private ShopCartFragment shopCartFragment;
    private Context context;
    private LayoutInflater layoutInflater;

    public CartAdapter(Context context, List<Cart> objects, ShopCartFragment shopCartFragment) {
        this.shopCartFragment = shopCartFragment;
        this.objects = objects;
        this.context = context;
        this.layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return objects.size();
    }

    @Override
    public Cart getItem(int position) {
        return objects.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.list_item_shopcart, null);
            convertView.setTag(new ViewHolder(convertView));
        }
        initializeViews((Cart)getItem(position), (ViewHolder) convertView.getTag(),position);
        return convertView;
    }

    private void initializeViews(Cart object, ViewHolder holder, final int position) {
        Picasso.get().load(MyTools.URL + "/" + object.getImg()).into(holder.imgMilkList);
        holder.tvMilktealistName.setText(object.getName());
        holder.tvMilktealistPrice.setText(object.getPrice()+"");
        holder.btnDelshopcart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "onClick: 点击了删除");
                FindCartGsonBean.Cart cart = shopCartFragment.cartList.get(position);
                shopCartFragment.deleteCart(cart);
            }
        });
    }

    protected class ViewHolder {
        private ImageView imgMilkList;
    private TextView tvMilktealistName;
    private TextView tvMilktealistPrice;
    private Button btnDelshopcart;

        public ViewHolder(View view) {
            imgMilkList = (ImageView) view.findViewById(R.id.img_milk_list);
            tvMilktealistName = (TextView) view.findViewById(R.id.tv_milktealist_name);
            tvMilktealistPrice = (TextView) view.findViewById(R.id.tv_milktealist_price);
            btnDelshopcart = (Button) view.findViewById(R.id.btn_delshopcart);
        }
    }
}
